package Client; //done

import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Common.Settings;
import GUI.LoginMenu;

/**
 * This is the main class of Client.
 * @author waleed
 */
public class CCRM_Client_Main {
    /**
     * Client main function.
     * @param args
     */
    public static void main(String[] args){
    final ClientLoginWindow clientLoginWindow = new ClientLoginWindow();
    clientLoginWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    clientLoginWindow.setBounds(Settings.Screen_x, Settings.Screen_y, 400, 300);
    clientLoginWindow.getStartButton().addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent e) {	
			 int port = 0; //Port to listen on
			 try{
			     	port = clientLoginWindow.getPort(); //Get port from command line
			     	if (port == -1 || port < 0){
			     		JOptionPane.showMessageDialog(null, "Ilegal port number");
			     		port = Settings.Default_Port; //Set port to 5555
			     	}
			     	if (port == 0)
			     		port = Settings.Default_Port; //Set port to 5555				     		
			    }
			    catch(Throwable t){
			    	port = Settings.Default_Port; //Set port to 5555
			    }				    
			    String  host = clientLoginWindow.getIP();
			    
			    try {
			    	CCRM_Client_Controller ccrm_Client_Controller= new CCRM_Client_Controller(host, port);
			    	ConnectionToServer.setClient(ccrm_Client_Controller);	      
			    	LoginMenu loginMenu = new LoginMenu();
			    	loginMenu.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
			    	loginMenu.setBounds(Settings.Screen_x, Settings.Screen_y, 600, 480);
			    	//ClientStartForm client_startForm = new ClientStartForm(port);
			    	//clientLoginGUI.setContentPane(client_startForm);
			    	loginMenu.setVisible( true ); // display frame
			    	clientLoginWindow.setVisible(false);
			    } 
			    catch(IOException exception) {
			    	JOptionPane.showMessageDialog(null, "Error: Can't setup connection!"
			                + " Terminating client.");
			      System.out.println("Error: Can't setup connection!"
			                + " Terminating client.");
			      System.exit(1);
			    }
			    catch (Exception errorexceptio){
			    	JOptionPane.showMessageDialog(null, "FATAL ERROR");//ERROR
			    }				   		    
		}
	});	    
    clientLoginWindow.setVisible(true);
    }
}
